package io.renren.modules.project.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.modules.project.service.ChartOutputService;
import io.renren.modules.project.vo.ChartOutputVoEntity;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产值统计表
 *
 * @author ygg
 * @date 2019-11-18 15:04:00
 */
@RestController
@RequestMapping("project/chartoutput")
public class ChartOutputController {

    @Autowired
    private ChartOutputService chartOutputService;

    /**
     * 作业/质检 产值列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("project:chartoutput")
    public R wlist(@RequestParam Map<String, Object> params){
        List<ChartOutputVoEntity> list = chartOutputService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 导出产值统计表Excel (作业/质检)
     */
    @RequestMapping("/exportExcel")
    public R exportExcel(HttpServletResponse response, @RequestParam Map<String, Object> params){
        try {
            HSSFWorkbook workbook = chartOutputService.exportExcel(params);
            //准备将Excel的输出流通过response输出到页面下载
            response.setContentType("application/vnd.ms-excel");
            //设置导出Excel的名称
            response.setHeader("Content-disposition", "attachment;filename=" + params.get("title") + ".xls");
            //刷新缓冲
            response.flushBuffer();
            //workbook将Excel写入到response的输出流中，供页面下载该Excel文件
            workbook.write(response.getOutputStream());
            //关闭workbook
            workbook.close();
        } catch (Exception ex) {
            return  R.error(ex.toString());
        }
        return R.ok();
    }

}
